﻿module Sustainalytics.Universes {
    export class UniversesController {
        public selectedFile: any;
        public isProcessingScheduled: boolean = false;
        public uploadErrorLogs: Array<IUploadErrorLog>;
        public loading: boolean;
        public $scope: ng.IScope;
        public universesService: IUniversesService;
        public $rootScope: any;
        public $q: ng.IQService;
        public fileCorrect: boolean;
        public notificationFactory: INotificationFactory;

        constructor($scope: ng.IScope, $rootScope, $q: ng.IQService, $location: ng.ILocationService, $http: ng.IHttpService, UniversesService, notificationFactory: INotificationFactory, staticObjects) {
            $scope['universes'] = this;
            
            this.$scope = $scope;
            this.$rootScope = $rootScope;
            this.universesService = UniversesService;
            this.notificationFactory = notificationFactory;
            this.$q = $q;

            this.universesService.getUploadErrorLogs()
                .then(data => {
                    this.uploadErrorLogs = data;
                });
            this.universesService.isProcessingScheduled()
                .then(data => {
                    this.isProcessingScheduled = data
                });
        }

        public selectFile(files) {

            var ctx = this;
            // is always one file - from html 
            var file = files[0];
            if (file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {

                var def = ctx.$q.defer();
                var reader = new FileReader();
                reader.onload = () => {
                    ctx.$scope.$evalAsync(() => {
                        ctx.selectedFile = file;
                    });
                    def.resolve(true);
                }
                reader.readAsDataURL(file);// read like this in order to display 
                ctx.fileCorrect = true;
                ctx.$q.all([def.promise]);
            } else {
                ctx.fileCorrect = false;
                ctx.notificationFactory.error('', ctx.getErrorMessage(2000));
            }
        }

        private uploadFile(): ng.IPromise<{}> {
            var ctx = this;
            var promise = ctx.universesService.fileUpload(ctx.selectedFile);
            promise.then(
                //scuccess
                () => {
                    ctx.$scope.$evalAsync(() => {
                        ctx.isProcessingScheduled = true;
                        ctx.notificationFactory.success('Upload succeeded.');
                    });
                },
                //error
                (error) => {
                    ctx.notificationFactory.error("Error uploading the file.", ctx.getErrorMessage(1000));
                });
            return promise;
        }

        private getErrorMessage(errorStatusCode: number) {
            var errorMessage = "";

            switch (errorStatusCode) {
                case 1000:
                    errorMessage = "Unexpected error encountered on the server while trying to process the uploaded file!";
                    break;
                case 2000:
                    errorMessage = "The selected file is invalid!";
                    break;
                default:
                    errorMessage = "There was an error encountered while trying to upload the file selected!";
            }

            return errorMessage;
        }
    }

    // register controller with Tools Angular module
    angular.module('Sustainalytics.Universes').controller('UniversesController', ($scope: ng.IScope, $rootScope, $q: ng.IQService, $location: ng.ILocationService, $http: ng.IHttpService, UniversesService, notificationFactory: INotificationFactory, staticObjects: IStaticObjects) => new UniversesController($scope, $rootScope, $q, $location, $http, UniversesService, notificationFactory, staticObjects));
} 